package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.FtpUtils;
import common.Pagination;
import entity.CroPropaganda;
import entity.SysCountry;
import entity.SysDict;
import org.springframework.beans.factory.annotation.Autowired;
import service.*;
import vo.CroPurgoodsVo;
import vo.CroPurgoodsVos;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import entity.SysDict;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.SysDictService;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * Created by xuweijie on 17-5-15.
 */
public class IndexAction extends ActionSupport {
    private CroPurgoodsVo croPurgoodsVo;
    private CroPurgoodsVos croPurgoodsVos;
    private List<CroPurgoodsVo> croPurgoodsVoList;
    private List<CroPropaganda> propagandaList;
    private List<CroPropaganda> friendUrlList;
    private String search;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 12;
    private File upload;

    @Autowired
    private CroPurgoodsService croPurgoodsService;
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysCountryService sysCountryService;
    @Autowired
    private CroPropagandaService croPropagandaService;


    /**
     * 显示前台index
     * */
    public String showindex(){
        ServletContext context= ServletActionContext.getServletContext();
        if (context.getAttribute("SysDict")==null){
            List<SysDict> list = sysDictService.lists();
            Map<String,Map<String,String>> maps = new HashMap<String,Map<String,String>>();
            for (SysDict dict : list) {
                maps.computeIfAbsent(dict.getLabel(), k -> new HashMap<String,String>());
                maps.get(dict.getLabel()).put(dict.getType(),dict.getValue());
            }
            context.setAttribute("SysDict",maps);
        }
        String areajson= (String) context.getAttribute("areajson");
        if(areajson==null || areajson.equals("")){
            List<SysCountry> countries=sysCountryService.list();
            areajson=JSON.toJSONString(countries);
            context.setAttribute("areajson",areajson);
        }
        List<CroPropaganda> friendurl= (List<CroPropaganda>) context.getAttribute("friendurl");
        if(friendurl==null){
            friendUrlList=croPropagandaService.listfriendurl();
            context.setAttribute("friendurl",friendUrlList);
        }

        Pagination<CroPurgoodsVo> page = new Pagination<CroPurgoodsVo>();
        if (pageIndex == 0) {
            page = croPurgoodsService.listsPage(1, pageSize);
        } else {
            page = croPurgoodsService.listsPage(pageIndex, pageSize);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        croPurgoodsVoList = page.getItems();
        int k = 0;
        for (CroPurgoodsVo croPurgoodsVo : croPurgoodsVoList) {
            CroPurgoodsVos croPurgoodsVos = new CroPurgoodsVos();
            String json = croPurgoodsVo.getDescription().toString();
            croPurgoodsVos = JSON.parseObject(json, CroPurgoodsVos.class);
            croPurgoodsVoList.get(k).setImagurl(croPurgoodsVos.getPicUrl());
            croPurgoodsVoList.get(k).setUniprice(croPurgoodsVos.getPrice());
            k++;
        }
        propagandaList = croPropagandaService.listCarousel();
        return SUCCESS;
    }

    /*
    * 分类查询
    * */
    public String getGoodsByCondiction() {
        System.out.println("getGoodsByCondictionAction" + croPurgoodsVo.getType());
        if(croPurgoodsVo.getType().equals("-1")){
            croPurgoodsVo.setType(null);
        }
        Pagination<CroPurgoodsVo> page = new Pagination<CroPurgoodsVo>();
        if (pageIndex == 0) {
            page = croPurgoodsService.listsPageByConditcion(1,pageSize,null,croPurgoodsVo.getType(),null,null);
        } else {
            page = croPurgoodsService.listsPageByConditcion(1,pageSize,null,croPurgoodsVo.getType(),null,null);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        croPurgoodsVoList = page.getItems();
        int k = 0;
        for (CroPurgoodsVo croPurgoodsVo : croPurgoodsVoList) {
            CroPurgoodsVos croPurgoodsVos = new CroPurgoodsVos();
            String json = croPurgoodsVo.getDescription().toString();
            croPurgoodsVos = JSON.parseObject(json, CroPurgoodsVos.class);
            croPurgoodsVoList.get(k).setImagurl(croPurgoodsVos.getPicUrl());
            croPurgoodsVoList.get(k).setUniprice(croPurgoodsVos.getPrice());
            k++;
        }
        propagandaList = croPropagandaService.listCarousel();
        return SUCCESS;
    }

    /*
    * search
    * */
    public String getGoodsBySerch(){
        System.out.println("getGoodsBySerchAction:"+search);
        Pagination<CroPurgoodsVo> page = new Pagination<CroPurgoodsVo>();
        if (pageIndex == 0) {
            page = croPurgoodsService.frontListsPageByConditcion(1,pageSize,search);
        } else {
            page = croPurgoodsService.frontListsPageByConditcion(pageIndex,pageSize,search);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        croPurgoodsVoList = page.getItems();
        int k = 0;
        for (CroPurgoodsVo croPurgoodsVo : croPurgoodsVoList) {
            CroPurgoodsVos croPurgoodsVos = new CroPurgoodsVos();
            String json = croPurgoodsVo.getDescription().toString();
            croPurgoodsVos = JSON.parseObject(json, CroPurgoodsVos.class);
            croPurgoodsVoList.get(k).setImagurl(croPurgoodsVos.getPicUrl());
            croPurgoodsVoList.get(k).setUniprice(croPurgoodsVos.getPrice());
            k++;
        }
        propagandaList = croPropagandaService.listCarousel();
        return SUCCESS;
    }

    /*
    * 商品详情
    * */
    public String showGoodsDetail(){
        return "detail";
    }

    /*
    * 测试上传
    * */
    public String test(){
        return "test";
    }
    public String uploadAction(){
        System.out.println("file"+upload);
//        System.out.println("path"+upload.getAbsolutePath()+"name:"+upload.getName());
        try{
            FtpUtils.upload(null, "bxu2359600628.my3w.com", "bxu2359600628", 21, "xingxing8", "/htdocs", upload, "1234567.jpg");
        }catch (Exception e){

        }
        System.out.println("url:"+"bxu2359600628.my3w.com/1234567.jpg");

        return "test";
    }


    public CroPurgoodsVo getCroPurgoodsVo() {
        return croPurgoodsVo;
    }

    public void setCroPurgoodsVo(CroPurgoodsVo croPurgoodsVo) {
        this.croPurgoodsVo = croPurgoodsVo;
    }

    public List<CroPurgoodsVo> getCroPurgoodsVoList() {
        return croPurgoodsVoList;
    }

    public void setCroPurgoodsVoList(List<CroPurgoodsVo> croPurgoodsVoList) {
        this.croPurgoodsVoList = croPurgoodsVoList;
    }

    public CroPurgoodsVos getCroPurgoodsVos() {
        return croPurgoodsVos;
    }

    public void setCroPurgoodsVos(CroPurgoodsVos croPurgoodsVos) {
        this.croPurgoodsVos = croPurgoodsVos;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public List<CroPropaganda> getPropagandaList() {
        return propagandaList;
    }

    public void setPropagandaList(List<CroPropaganda> propagandaList) {
        this.propagandaList = propagandaList;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public List<CroPropaganda> getFriendUrlList() {
        return friendUrlList;
    }

    public void setFriendUrlList(List<CroPropaganda> friendUrlList) {
        this.friendUrlList = friendUrlList;
    }
}
